package com.itmk.web.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.stu_role.entity.StuRole;
import com.itmk.web.stu_role.service.StuRoleService;
import com.itmk.web.student.entity.StuParm;
import com.itmk.web.student.entity.Student;
import com.itmk.web.student.service.StudentService;
import com.itmk.web.teacher.entity.Teacher;
import com.itmk.web.teacher_role.entity.TeacherRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author java实战基地
 * @Version 2383404558
 */
@RestController
@RequestMapping("/api/student")
public class StudentController {
     @Autowired
    private StudentService studentService;
     @Autowired
     private StuRoleService stuRoleService;

    //新增
    @PostMapping
    public ResultVo add(@RequestBody Student student){
        //学号重复判断
        QueryWrapper<Student> query = new QueryWrapper<>();
        query.lambda().eq(Student::getStuNum,student.getStuNum());
        Student one = studentService.getOne(query);
        if(one != null){
            return ResultUtils.error("学号被占用!");
        }
        //设置密码
        student.setPassword(DigestUtils.md5DigestAsHex(student.getPassword().getBytes()));
        studentService.addStu(student);
        return ResultUtils.success("新增成功");
    }


    //编辑
    @PutMapping
    public ResultVo edit(@RequestBody Student student){
        //学号重复判断
        QueryWrapper<Student> query = new QueryWrapper<>();
        query.lambda().eq(Student::getStuNum,student.getStuNum());
        Student one = studentService.getOne(query);
        if(one != null && !student.getStuId().equals(one.getStuId())){
            return ResultUtils.error("学号被占用!");
        }
        studentService.editStu(student);
        return ResultUtils.success("编辑成功");
    }

    //删除
    @DeleteMapping("/{stuId}")
    public ResultVo delete(@PathVariable("stuId") Long stuId){
        studentService.deleteStu(stuId);
        return ResultUtils.success("删除成功");
    }

    //列表
    @GetMapping("/list")
    public ResultVo list(StuParm parm){
//        IPage<Student> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
//        QueryWrapper<Student> query = new QueryWrapper<>();
//        if(StringUtils.isNotEmpty(parm.getStuName())){
//            query.lambda().like(Student::getStuName,parm.getStuName());
//        }
        IPage<Student> list = studentService.getList(parm);
        return ResultUtils.success("查询成功",list);
    }

    //根据教师id查询教师信息
    @GetMapping("/getStudent")
    public ResultVo getStudent(Long stuId){
        Student student = studentService.getById(stuId);
        QueryWrapper<StuRole> query = new QueryWrapper<>();
        query.lambda().eq(StuRole::getStuId,stuId);
        StuRole one = stuRoleService.getOne(query);
        student.setRoleId(one.getRoleId());
        return ResultUtils.success("查询成功",student);
    }
    //学生重置密码
    @PostMapping("/resetPassword")
    public ResultVo resetPassword(@RequestBody Student student){
        //设置密码
        String password = DigestUtils.md5DigestAsHex("666666".getBytes());
        student.setPassword(password);
        studentService.updateById(student);
        return ResultUtils.success("重置密码成功");
    }
}
